<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--
 * ${table.mapperName}
 * ${table.tableComment}
 * @author  ${config.author}
 * @version ${config.version}
 * 描述：${config.description}
 * 时间：${table.genDateTime?string("yyyy-MM-dd HH:mm:ss")}
-->
<mapper namespace="com.cyh.ebptpubjar.${table.packageName}.dao.${table.daoName}" >

    <resultMap id="${table.entityName}" type="com.cyh.ebptpubjar.${table.packageName}.entity.${table.entityNameWithEntity}" >
        <#list columns as column>
            <#if column.primary==1>
                <id column="${column.dbName}" property="${column.name}" jdbcType="${column.jdbcType}" /><!--${column.comment}-->
            </#if>
        </#list>
        <#list columns as column>
            <#if column.primary==0>
                <result column="${column.dbName}" property="${column.name}" jdbcType="${column.jdbcType}" /><!--${column.comment}-->
            </#if>
        </#list>
    </resultMap>

    <!-- 以下引用sql -->
    <#if table.primaryKeyIsNotEmpty==1>
    <!-- 主键条件 -->
    <sql id="primaryCondition">
        <where>
            <#list primaryColumns as column>
                <#if column_index != 0> AND </#if>${column.dbName} = ${'#'}${'{'}${table.entityNameLowerName}.${column.name}, jdbcType=${column.jdbcType}${'}'}
            </#list>
        </where>
    </sql>
    </#if>
    <!-- 全部列名 -->
    <sql id="baseColumn" >
        <#list columns as column>
            ${column.dbName}<#if column_has_next>,</#if>
        </#list>
    </sql>
    <!-- 通用条件判断 -->
    <sql id="whereCondition">
        <where>
            <#list columns as column>
                <#if column.javaType == 'Date'
                || column.javaType == 'Boolean'
                || column.javaType == 'Time'
                || column.javaType == 'Timestamp'>
                    <if test="${table.entityNameLowerName}.${column.name} != null" >
                        AND ${column.dbName} = ${'#'}${'{'}${table.entityNameLowerName}.${column.name}, jdbcType=${column.jdbcType}${'}'}
                    </if>
                <#else>
                    <if test="${table.entityNameLowerName}.${column.name} != null and ${table.entityNameLowerName}.${column.name} != ''" >
                        AND ${column.dbName} = ${'#'}${'{'}${table.entityNameLowerName}.${column.name}, jdbcType=${column.jdbcType}${'}'}
                    </if>
                </#if>
            </#list>
        </where>
    </sql>

    <#if table.primaryKeyIsNotEmpty==1>
    <sql id="orderByKeyDesc">
        ORDER BY
        <#list primaryColumns as column>
            ${column.dbName}<#if column_has_next>,</#if>
        </#list>
        DESC
    </sql>
    </#if>

    <!-- 插入数据 -->
    <insert id="insert">
        INSERT INTO ${table.tableNameUpper}
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <#list columns as column>
                <#if column.javaType == 'Date'
                || column.javaType == 'Boolean'
                || column.javaType == 'Time'
                || column.javaType == 'Timestamp'>
                    <if test="${table.entityNameLowerName}.${column.name} != null" >
                        ${column.dbName},
                    </if>
                <#else>
                    <if test="${table.entityNameLowerName}.${column.name} != null and ${table.entityNameLowerName}.${column.name} != ''" >
                        ${column.dbName},
                    </if>
                </#if>
            </#list>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides="," >
            <#list columns as column>
                <#if column.javaType == 'Date'
                || column.javaType == 'Boolean'
                || column.javaType == 'Time'
                || column.javaType == 'Timestamp'>
                    <if test="${table.entityNameLowerName}.${column.name} != null" >
                        ${'#'}${'{'}${table.entityNameLowerName}.${column.name}, jdbcType=${column.jdbcType}${'}'},
                    </if>
                <#else>
                    <if test="${table.entityNameLowerName}.${column.name} != null and ${table.entityNameLowerName}.${column.name} != ''" >
                        ${'#'}${'{'}${table.entityNameLowerName}.${column.name}, jdbcType=${column.jdbcType}${'}'},
                    </if>
                </#if>
            </#list>
        </trim>
    </insert>
    <!-- 批量插入数据 -->
    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO ${table.tableNameUpper}
        (
            <include refid="baseColumn" />
        )
        VALUES
        <foreach collection="${table.entityNameLowerName}List" item="item" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides="," >
                <#list columns as column>
                    ${'#'}${'{'}item.${column.name}, jdbcType=${column.jdbcType}${'}'},
                </#list>
            </trim>
        </foreach>
    </insert>

    <#if table.primaryKeyIsNotEmpty==1>
    <!-- 根据主键修改数据 -->
    <update id="updateByKey">
        UPDATE ${table.tableNameUpper}
        <set>
            <#list columns as column>
                <#if column.javaType == 'Date'
                || column.javaType == 'Boolean'
                || column.javaType == 'Time'
                || column.javaType == 'Timestamp'>
                    <if test="${table.entityNameLowerName}.${column.name} != null" >
                        ${column.dbName} = ${'#'}${'{'}${table.entityNameLowerName}.${column.name}, jdbcType=${column.jdbcType}${'}'},
                    </if>
                <#else>
                    <if test="${table.entityNameLowerName}.${column.name} != null and ${table.entityNameLowerName}.${column.name} != ''" >
                        ${column.dbName} = ${'#'}${'{'}${table.entityNameLowerName}.${column.name}, jdbcType=${column.jdbcType}${'}'},
                    </if>
                </#if>
            </#list>
        </set>
        <include refid="primaryCondition" />
    </update>
    </#if>
    <#if table.primaryKeyIsNotEmpty==1>
    <!-- 根据主键删除数据 -->
    <delete id="deleteByKey">
        DELETE FROM ${table.tableNameUpper}
        <include refid="primaryCondition" />
    </delete>
    </#if>

    <!--通过条件批量删除数据-->
    <delete id="deleteBatchByCondition" parameterType="java.util.List">
        DELETE FROM ${table.tableNameUpper}
        <where>
            <foreach collection="${table.entityNameLowerName}List" item="item" separator=" OR ">
                (
                    <trim prefix="" suffix="" suffixOverrides="AND" >
                    <#list columns as column>
                        <#if column.javaType == 'Date'
                        || column.javaType == 'Boolean'
                        || column.javaType == 'Time'
                        || column.javaType == 'Timestamp'>
                            <if test="item.${column.name} != null" >
                                 ${column.dbName} = ${'#'}${'{'}item.${column.name}, jdbcType=${column.jdbcType}${'}'} AND
                            </if>
                        <#else>
                            <if test="item.${column.name} != null and item.${column.name} != ''" >
                                 ${column.dbName} = ${'#'}${'{'}item.${column.name}, jdbcType=${column.jdbcType}${'}'} AND
                            </if>
                        </#if>
                    </#list>
                    </trim>
                )
            </foreach>
        </where>
    </delete>
    <!-- 自定义删除数据 -->
    <delete id="deleteByCondition">
        DELETE FROM ${table.tableNameUpper}
        <include refid="whereCondition" />
    </delete>

    <!-- 根据条件查询数量 -->
    <select id="selectCountByCondition" resultType="java.lang.Integer" >
        SELECT count(1) FROM ${table.tableNameUpper}
        <include refid="whereCondition" />
    </select>
    <#if table.primaryKeyIsNotEmpty==1>
    <select id="getByKey" resultMap="${table.entityName}" >
        SELECT <include refid="baseColumn" /> FROM ${table.tableNameUpper}
        <include refid="primaryCondition"/>
    </select>
    </#if>
    <select id="getByCondition" resultMap="${table.entityName}" >
        SELECT <include refid="baseColumn" /> FROM ${table.tableNameUpper}
        <include refid="whereCondition" />
            <#if table.primaryKeyIsNotEmpty==1>
                <include refid="orderByKeyDesc" />
            </#if>
    </select>

    <!-- 根据条件批量查询 -->
    <select id="getBatchByCondition" parameterType="java.util.List" resultMap="${table.entityName}">
        SELECT <include refid="baseColumn" /> FROM ${table.tableNameUpper}
        <where>
            <foreach collection="${table.entityNameLowerName}List" item="item" separator=" OR ">
                (
                    <trim prefix="" suffix="" suffixOverrides="AND" >
                    <#list columns as column>
                        <#if column.javaType == 'Date'
                        || column.javaType == 'Boolean'
                        || column.javaType == 'Time'
                        || column.javaType == 'Timestamp'>
                            <if test="item.${column.name} != null" >
                                ${column.dbName} = ${'#'}${'{'}item.${column.name}, jdbcType=${column.jdbcType}${'}'} AND
                            </if>
                        <#else>
                            <if test="item.${column.name} != null and item.${column.name} != ''" >
                                ${column.dbName} = ${'#'}${'{'}item.${column.name}, jdbcType=${column.jdbcType}${'}'} AND
                            </if>
                        </#if>
                    </#list>
                    </trim>
                )
            </foreach>
        </where>
    </select>
</mapper>